#include "wiicore.h"
#include "gfx.h"
#include "gamecore.h"
GRRLIB_texImg*titlescreenimg,*gametypechooseimg,*p1cursimg;
enum gamestruct gamestate=titlescreen;
enum gametypestruct gametype=ffa;
playerstruct p1,p2,p3,p4;
int playernum=2;
int main(){
  WPAD_Init();
  WPAD_SetVRes(0,640+100,480+100);
  WPAD_SetDataFormat(WPAD_CHAN_ALL,WPAD_FMT_BTNS_ACC_IR);
  GRRLIB_Init();
  ir_t empty;
  p1.create(WPAD_ButtonsDown(0),WPAD_ButtonsHeld(0),empty);
  p2.create(WPAD_ButtonsDown(1),WPAD_ButtonsHeld(1),empty);
  p3.create(WPAD_ButtonsDown(2),WPAD_ButtonsHeld(2),empty);
  p4.create(WPAD_ButtonsDown(3),WPAD_ButtonsHeld(3),empty);
  while(1){
    WPAD_ScanPads();
    p1.down=WPAD_ButtonsDown(0);p1.held=WPAD_ButtonsHeld(0);
    p2.down=WPAD_ButtonsDown(1);p2.held=WPAD_ButtonsHeld(1);
    p3.down=WPAD_ButtonsDown(2);p3.held=WPAD_ButtonsHeld(2);
    p4.down=WPAD_ButtonsDown(3);p4.held=WPAD_ButtonsHeld(3);
    WPAD_IR(0,&p1.ir);WPAD_IR(1,&p2.ir);WPAD_IR(2,&p3.ir);WPAD_IR(3,&p4.ir);
    if(p1.down&WPAD_BUTTON_HOME){
      GRRLIB_Exit();
      exit(0);
    }
    if(gamestate==titlescreen){
      titlescreenimg=GRRLIB_LoadTexturePNG(titlescreen_png);
      GRRLIB_DrawImg(0,0,titlescreenimg,0,1,1,color_white);
      if(p1.down&WPAD_BUTTON_PLUS){
        GRRLIB_FreeTexture(titlescreenimg);
        gamestate=gametypechoose;
      }
    }
    else if(gamestate==gametypechoose){
      gametypechooseimg=GRRLIB_LoadTexturePNG(gametypechoose_png);
      p1cursimg=GRRLIB_LoadTexturePNG(p1curs_png);
      GRRLIB_DrawImg(0,0,gametypechooseimg,0,1,1,color_white);
      GRRLIB_DrawImg(p1.ir.x,p1.ir.y,p1cursimg,0,1,1,color_white);
      if(p1.down&WPAD_BUTTON_A){
        //TODO:Add FFA box coords.
        if(GRRLIB_PtInRect(p1.ir.x,p1.ir.y,x,y,w,h)){
          gametype=ffa;
          GRRLIB_FreeTexture(gametypechooseimg);
          GRRLIB_FreeTexture(p1cursimg);
          gamestate=playernumchoose;
        }
        //TODO:Add 2v2 box coords.
        else if(GRRLIB_PtInRect(p1.ir.x,p1.ir.y,x,y,w,h)){
          gametype=2v2;
          gamestate=playernumchoose;
          GRRLIB_FreeTexture(gametypechooseimg);
          GRRLIB_FreeTexture(p1cursimg);
        }
        //TODO:Add 3v1 box coords.
        else if(GRRLIB_PtInRect(p1.ir.x,p1.ir.y,x,y,w,h)){
          gametype=3v1;
          gamestate=playernumchoose;
          GRRLIB_FreeTexture(gametypechooseimg);
          GRRLIB_FreeTexture(p1cursimg);
        }
        else{}
    }
    else if(gamestate==playernumchoose){
        playernumchooseimg=GRRLIB_LoadTexturePNG(playernumchoose_png);
        p1cursimg=GRRLIB_LoadTexturePNG(p1curs_png);
        GRRLIB_DrawImg(0,0,playernumchooseimg,0,1,1,color_white);
        GRRLIB_DrawImg(p1.ir.x,p1.ir.y,p1cursimg,0,1,1,color_white);
        if(p1.down&WPAD_BUTTON_A){
          //TODO:Add 2 players box coords.
          if(GRRLIB_PtInRect(p1.ir.x,p1.ir.y,x,y,w,h)){
            playernum=2;
            gamestate=mapchoose;
            GRRLIB_FreeTexture(playernumchooseimg);
            GRRLIB_FreeTexture(p1cursimg);
          }
          //TODO:Add 3 players box coords.
          if(GRRLIB_PtInRect(p1.ir.x,p1.ir.y,x,y,w,h)){
            playernum=3;
            gamestate=mapchoose;
            GRRLIB_FreeTexture(playernumchooseimg);
            GRRLIB_FreeTexture(p1cursimg);
          }
          //TODO:Add 4 players box coords.
          if(GRRLIB_PtInRect(p1.ir.x,p1.ir.y,x,y,w,h)){
            playernum=4;
            GRRLIB_FreeTexture(playernumchooseimg);
            GRRLIB_FreeTexture(p1cursimg);
            gamestate=mapchoose;
          }
          else{}
       }
    }
    GRRLIB_Render();
}
